package com.kancy.slimmaker.utils;


import com.kancy.slimmaker.log.Log;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>
 * AlertUtils
 * <p>
 *
 * @author: kancy
 * @date: 2020/6/10 19:58
 **/

public class AlertUtils {
    /**
     * 提示信息
     * @param msgFormat
     * @param args
     */
    public static void msg(String msgFormat, Object ... args){
        msg(JOptionPane.getRootFrame(), msgFormat, args);
    }

    /**
     * 提示信息
     * @param component
     * @param msgFormat
     * @param args
     */
    public static void msg(Component component, String msgFormat, Object ... args){
        if (Objects.nonNull(msgFormat)){
            String msg = String.format(msgFormat.replace("{}", "%s"), args);
            Log.warn(msg);
            JOptionPane.showMessageDialog(component, msg);
        }
    }

    public static boolean yesOrNo(String msgFormat, Object ... args){
        return yesOrNo(null, msgFormat, args);
    }

    public static boolean yesOrNo(Component component,String msgFormat, Object ... args){
        assert Objects.nonNull(msgFormat);
        String msg = String.format(msgFormat.replace("{}", "%s"), args);
        Log.warn(msg);
        int result = JOptionPane.showConfirmDialog(component, msg, "系统消息", JOptionPane.YES_NO_OPTION);
        boolean bResult = result == 0;
        Log.debug("用户选择：%s , %s", result, bResult);
        return bResult;
    }

    public static boolean verifyUserInput(Component component,String inputValue, String msgFormat, Object ... args){
        return Objects.equals(inputValue, getUserInput(component, msgFormat, args));
    }

    public static String getUserInput(Component component,String msgFormat, Object ... args){
        String msg = String.format(msgFormat.replace("{}", "%s"), args);
        Log.warn(msg);
        String userInputValue = JOptionPane.showInputDialog(component, msg);
        return userInputValue;
    }


    public static <T> T getUserChooseInput(Component component, T[] options, String msgFormat){
        return getUserChooseInput(component, options, null, msgFormat, null);
    }

    public static <T> T getUserChooseInput(Component component, T[] options, T initValue, String msgFormat, Object ... args){
        String msg = String.format(msgFormat.replace("{}", "%s"), args);
        Log.warn(msg);

        // 添加自定义组件
        JComboBox<T> objectJComboBox = new JComboBox<>();
        objectJComboBox.setEditable(true);
        JButton jButton = new JButton("确认");
        ArrayList<T> selectValue = new ArrayList<>(1);
        jButton.addActionListener(e -> {
            selectValue.add((T) objectJComboBox.getSelectedItem());
            JOptionPane.getRootFrame().dispose();
        });

        // 填充数据
        for (T option : options) {
            objectJComboBox.addItem(option);
        }
        if (Objects.nonNull(initValue)){
            objectJComboBox.removeItem(initValue);
            objectJComboBox.insertItemAt(initValue,0);
        }
        objectJComboBox.setSelectedIndex(0);

        // 显示面板
        JOptionPane.showOptionDialog(null, new Object[]{msg, objectJComboBox,jButton}, "系统输入",
                JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
        T userInputValue = selectValue.isEmpty() ? null : selectValue.get(0);
        Log.debug("用户输入：%s", userInputValue);
        return userInputValue;
    }

}
